package com.yanqu.road.entity.union;

import com.yanqu.road.utils.date.DateHelper;

import java.util.Calendar;
import java.util.Date;

public class UnionWarTime {
    private int startDay;
    private int startHour;
    private int startMinute;
    private int endDay;
    private int endHour;
    private int endMinute;
    private boolean crossWeek;
    private UnionWarTime prepareTime; //周期开始的时间段
    private UnionWarTime rewardTime; //周期结束的时间段

    /**
     * 判断是否在这个过程中
     * @return
     * @param time
     */
    public boolean isInTime(long time) {
        return time >= getStartTime(time) && time < getEndTime(time);
    }

    /**
     * 获取开始时间
     * @return
     */
    public long getStartTime(long nowTime) {
        long mondayTime = DateHelper.getThisWeekMonday(new Date(nowTime)).getTime();
        long startTime = mondayTime + (startDay - 1) * 24 * 3600 * 1000 + startHour * 3600 * 1000 + startMinute * 60 * 1000;
        if (isCrossWeek()) {
            // 跨周情况处理
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(nowTime);
            int day = DateHelper.getTodayWeek(cal);
            int hour =  cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);

            if (isEntityCrossWeek()) {
                // 整体跨周小阶段跨周

                // 0点左侧获取到的时间是正确 不需要处理

                if (isSmallThanAwardEnd(day, hour, minute)) {
                    // 周一0点右侧
                    // 获取到的startTime 多出7天 应该减7天的时间
                    startTime = startTime - 7 * 24 * 3600 * 1000;
                }
            } else {
                // 整体跨周 小阶段不跨周
                if (startDay > rewardTime.getEndDay() || (startDay == rewardTime.getEndDay() && startHour > rewardTime.getEndHour()) ||
                        (startDay == rewardTime.getEndDay() && startHour == rewardTime.getEndHour() && startMinute >= rewardTime.getEndMinute())) {
                    // 小阶段在0点左侧
//                    if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
//                            (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())) {
//                        //判断周一0点右侧的时间 应该 - 7 天
//                        startTime = startTime - 7 * 24 * 3600 * 1000;
//                    }
                    if (isSmallThanAwardEnd(day, hour, minute)) {
                        startTime = startTime - 7 * 24 * 3600 * 1000;
                    }

                } else {
                    // 小阶段在0点右侧 判断周一0点左侧的时间 应该+ 7 天
//                    if (day > prepareTime.getStartDay() || (day == prepareTime.getStartDay() && hour > prepareTime.getStartHour()) ||
//                            (day == prepareTime.getStartDay() && hour == prepareTime.getStartHour() && minute >= prepareTime.getStartMinute())) {
//                        startTime = startTime + 7 * 24 * 3600 * 1000;
//                    }
                    if (isBiggerThanPrepareStart(day, hour, minute)) {
                        startTime = startTime + 7 * 24 * 3600 * 1000;
                    }
                }
            }

//            if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
//                    (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())) {
//                startTime = startTime - 7 * 24 * 3600 * 1000;
//            }
        }
        return startTime;
    }

    public boolean isBiggerThanPrepareStart(int day, int hour, int minute) {
        if (day > prepareTime.getStartDay() || (day == prepareTime.getStartDay() && hour > prepareTime.getStartHour()) ||
                (day == prepareTime.getStartDay() && hour == prepareTime.getStartHour() && minute >= prepareTime.getStartMinute())) {
            return true;
        }
        return false;
    }

    public boolean isSmallThanAwardEnd(int day, int hour, int minute) {
        if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
                (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())) {
            //判断周一0点右侧的时间 应该 - 7 天
            return true;
        }
        return false;
    }

    public boolean isSmallThanEnd(int day, int hour, int minute) {
        if (day < endDay || (day == endDay && hour < endHour) || (day == endDay && hour == endHour && minute < endMinute)) {
            return true;
        }
        return false;
    }

    public boolean isBiggerThanStart(int day, int hour, int minute) {
        if (day > startDay || (day == startDay && hour > startHour) || (day == startDay && hour == startHour && minute >= startMinute)) {
            return true;
        }
        return false;
    }

    /**
     * 小阶段跨服
     * @return
     */
    public boolean isEntityCrossWeek() {
        if (getStartDay() > getEndDay() ||
                (getStartDay() == getEndDay() && getStartHour() > getEndHour()) ||
                (getStartDay() == getEndDay() && getStartHour() == getEndHour() && getStartMinute() >= getEndMinute())) {
            return true;
        }
        return false;
    }

    /**
     * 获取结束时间
     * @return
     */
    public long getEndTime(long nowTime) {
        long mondayTime = DateHelper.getThisWeekMonday(new Date(nowTime)).getTime();
        long endTime = mondayTime + (endDay - 1) * 24 * 3600 * 1000 + endHour * 3600 * 1000 + endMinute * 60 * 1000;
        if (isCrossWeek()) {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(nowTime);
            int day = DateHelper.getTodayWeek(cal);
            int hour =  cal.get(Calendar.HOUR_OF_DAY);
            int minute = cal.get(Calendar.MINUTE);
            if (isEntityCrossWeek()) {
                // 小阶段跨周 0点左侧 时间应该+7天
                if (isBiggerThanPrepareStart(day, hour, minute)) {
                    endTime = endTime + 7 * 24 * 3600 * 1000;
                }
                // 小阶段跨周 0点右侧时间正确 不需要处理

            } else {
                // 整体跨周 小阶段不跨周
                if (endDay > rewardTime.getEndDay() || (endDay == rewardTime.getEndDay() && endHour > rewardTime.getEndHour()) ||
                        (endDay == rewardTime.getEndDay() && endHour == rewardTime.getEndHour() && endMinute > rewardTime.getEndMinute())) {
                    // 小阶段在0点左侧
                    if (isSmallThanAwardEnd(day, hour, minute)) {
                        // 在0点右侧获取到的结束时间比正确时间多7天   左侧时间正确不处理
                        endTime = endTime - 7 * 24 * 3600 * 1000;
                    }

                } else {
                    // 小阶段在0点右侧 判断周一0点左侧的时间 应该+ 7 天
                   if (isBiggerThanPrepareStart(day, hour, minute)) {
                       endTime = endTime + 7 * 24 * 3600 * 1000;
                   }
                }
            }
        }

//        if (isCrossWeek() && endTime < prepareTime.getStartTime()) {
//            endTime += 7 * 24 * 3600 * 1000;
//        } else if (isCrossWeek()) {
//            Calendar cal = Calendar.getInstance();
//            cal.setTimeInMillis(nowTime);
//            int day = DateUtil.getTodayWeek(cal);
//            int hour =  cal.get(Calendar.HOUR_OF_DAY);
//            int minute = cal.get(Calendar.MINUTE);
//            if (day < rewardTime.getEndDay() || (day == rewardTime.getEndDay() && hour < rewardTime.getEndHour()) ||
//                    (day == rewardTime.getEndDay() && hour == rewardTime.getEndHour() && minute < rewardTime.getEndMinute())) {
//                endTime -= 7 * 24 * 3600 * 1000;
//            }
//        }
        return endTime;
    }


    public int getStartDay() {
        return startDay;
    }

    public void setStartDay(int startDay) {
        this.startDay = startDay;
    }

    public int getStartHour() {
        return startHour;
    }

    public void setStartHour(int startHour) {
        this.startHour = startHour;
    }

    public int getStartMinute() {
        return startMinute;
    }

    public void setStartMinute(int startMinute) {
        this.startMinute = startMinute;
    }

    public int getEndDay() {
        return endDay;
    }

    public void setEndDay(int endDay) {
        this.endDay = endDay;
    }

    public int getEndHour() {
        return endHour;
    }

    public void setEndHour(int endHour) {
        this.endHour = endHour;
    }

    public int getEndMinute() {
        return endMinute;
    }

    public void setEndMinute(int endMinute) {
        this.endMinute = endMinute;
    }

    public UnionWarTime getPrepareTime() {
        return prepareTime;
    }

    public void setPrepareTime(UnionWarTime prepareTime) {
        this.prepareTime = prepareTime;
    }

    public UnionWarTime getRewardTime() {
        return rewardTime;
    }

    public void setRewardTime(UnionWarTime rewardTime) {
        this.rewardTime = rewardTime;
    }

    public boolean isCrossWeek() {
        return crossWeek;
    }

    public void setCrossWeek(boolean crossWeek) {
        this.crossWeek = crossWeek;
    }

}
